<?php
/**
 * Created by PhpStorm.
 * User: lenovo-7
 * Date: 2015/6/16
 * Time: 11:43
 */

class inman_api_address extends inman_api_base {
    /**
     * 1.10 用户地址-删除地址
     */
    public function delete($params,&$service){
        $params = utils::_filter_input($params);//过滤xss攻击
        if(!$this->check_sid($params['sid']) ){
            return $service->send_user_error('100001','error sid');
        }
        $_GET['sess_id'] = $params['sid'];
        kernel::single("base_session")->start();
        $addrId = $params['address_id'];
        if(!$addrId){
            return $service->send_user_error('','参数错误，address_id不能为空');
        }
        $member_id = kernel::single('b2c_user_object')->get_member_id();
        $obj_member = app::get('b2c')->model('members');
        if($obj_member->check_addr($addrId,$member_id)){
            if($obj_member->del_rec($addrId,$message,$member_id)){
                $data['error_code'] = 0;
                echo $message;
            }else{
                $data['error_code'] = 1;
                echo $message;
            }
            return $data;
        }else{
            return $service->send_user_error('','用户地址不存在');
        }
    }

    /**
     * 1.11 用户地址-地址列表
     */
    public function getList($params,&$service){
        if(!$this->check_sid($params['sid']) ){
            return $service->send_user_error('100001','error sid');
        }
        $member_id = kernel::single('b2c_user_object')->get_member_id();
        $obj_member_addrs = app::get('b2c')->model('member_addrs');
        $address_list = $obj_member_addrs->getList('*',array('member_id'=>$member_id));
        foreach( (array)$address_list as $k=>$row ){

            $area_arr = explode(':',$row['area']);
            $area_name_arr = explode('/',$area_arr[1]);
            $regions = app::get('ectools')->model('regions');
            $res_row = $regions->getRow('region_path',array('region_id'=>$area_arr[2]));
            $region_path = $res_row['region_path'];
            $area_id_arr = explode(',',$region_path);

            $list['address_list'][$k]['address_id'] = $row['addr_id'];	    //  地址id
            $list['address_list'][$k]['consignee'] = $row['name'];	    //  收件人
            $list['address_list'][$k]['country_name'] = $area_arr[0] =='mainland'?'中国':'外国';	//  所在国家
            $list['address_list'][$k]['province_name'] = $area_name_arr[0];	//  所在省份
            $list['address_list'][$k]['city_name'] = $area_name_arr[1];	    //  所在城市
            $list['address_list'][$k]['district_name'] = $area_name_arr[2];	//  所在区县
            $list['address_list'][$k]['province'] = $area_id_arr[1];	    //	省份id
            $list['address_list'][$k]['city'] = $area_id_arr[2];	        //	城市id
            $list['address_list'][$k]['district'] = $area_id_arr[3];	    //	区县id
            $list['address_list'][$k]['address'] = $row['addr'];     	//	详细地址
            $list['address_list'][$k]['zipcode'] = $row['zip'];	        //	邮政编码
            $list['address_list'][$k]['tel'] = $row['tel'];	            //	电话
            $list['address_list'][$k]['phone'] = $row['mobile'];	        //	手机号码
            $list['address_list'][$k]['is_default'] = $row['def_addr'] ? 'true' : 'false';    //	是否为默认收货地址
            $list['address_list'][$k]['support_cod'] = '????';	        //	是否支持货到付款
        }
        return $list;
    }

    /**
     * 1.12 用户地址-更新收货地址
     */
    public function update($params,&$service){
        if(!$this->check_sid($params['sid']) ){
            return $service->send_user_error('100001','error sid');
        }
        $member_id = kernel::single('b2c_user_object')->get_member_id();
        $member_addrs = app::get('b2c')->model('member_addrs');
        $regions = app::get('ectools')->model('regions');

        $must_params = array('address_id');
        foreach($must_params as $must_params_v){
            if(empty($params[$must_params_v])){
                $msg = $must_params_v.' 参数为必填参数';
                return $service->send_user_error($msg);
            }
        }
        $addrs = $member_addrs->getRow('name,area,addr,zip,mobile',array('addr_id'=>$params['address_id']));
        $area_arr = explode(':',$addrs['area']);
        $area_name_arr = explode('/',$area_arr[1]);

        if($params['province']){
            $province_name_arr = $regions->getById($params['province']);
            $province_name = $province_name_arr['local_name'];
        }else{
            $province_name = $area_name_arr[0];
        }

        if($params['city']){
            $city_name_arr = $regions->getById($params['city']);
            $city_name = $city_name_arr['local_name'];
        }else{
            $city_name = $area_name_arr[1];
        }

        if($params['district']){
            $district_name_arr = $regions->getById($params['district']);
            $district_name = $district_name_arr['local_name'];
        }else{
            $district_name = $area_name_arr[2];
        }

        $data['addr_id']    = $params['address_id'];
        $data['name']       = $params['consignee']?$params['consignee']:$addrs['name'];
        $data['area']       = 'mainland:'.$province_name.'/'.$city_name.'/'.$district_name.':'.($params['district']?$params['district']:$area_arr[2]);
        $data['addr']       = $params['address']?$params['address']:$addrs['addr'];
        $data['zip']        = $params['zipcode']?$params['zipcode']:$addrs['zip'];
        $data['mobile']     = $params['mobile']?$params['mobile']:$addrs['mobile'];

        $save_data = kernel::single('b2c_member_addrs')->purchase_save_addr($data,$member_id,$msg);
        $return['error_code'] = $save_data ? 0 : 1;
        echo $msg;
        return $return;
    }

    /**
     * 1.13 用户地址-添加收货地址
     */
    public function add($params,&$service){
        if(!$this->check_sid($params['sid']) ){
            return $service->send_user_error('100001','error sid');
        }
        $member_id = kernel::single('b2c_user_object')->get_member_id();
        $regions = app::get('ectools')->model('regions');

        $must_params = array('consignee','mobile','province','city','district','address');
        foreach($must_params as $must_params_v){
            if(empty($params[$must_params_v])){
                $msg = $must_params_v.' 参数为必填参数';
                return $service->send_user_error($msg);
            }
        }
        $province_name_arr = $regions->getById($params['province']);
        $city_name_arr = $regions->getById($params['city']);
        $district_name_arr = $regions->getById($params['district']);
        $data['name']       = $params['consignee']  ;
        $data['area']       = 'mainland:'.$province_name_arr['local_name'].'/'.$city_name_arr['local_name'].'/'.$district_name_arr['local_name'].':'.$params['district'];
        $data['addr']       = $params['address']  ;
        $data['zip']        = $params['zipcode']   ;
        $data['mobile']     = $params['mobile'];
        $data['def_addr']   = 0;

        $save_data = kernel::single('b2c_member_addrs')->purchase_save_addr($data,$member_id,$msg);
        $return['address_id'] = $save_data['addr_id'];
        echo $msg;
        return $return;
    }

    /**
     * 1.14 用户地址-设置默认收货地址
     */
    public function setDefault($params,&$service){
        if(!$this->check_sid($params['sid']) ){
            return $service->send_user_error('100001','error sid');
        }
        $member_id = kernel::single('b2c_user_object')->get_member_id();
        $member_addrs = app::get('b2c')->model('member_addrs');
        if(empty($params['address_id'])){
            return $service->send_user_error('参数错误,address_id不能为空');
        }else{
            $addr_id = $params['address_id'];
        }
        if(!$member_addrs->getList('*',array('addr_id'=>$addr_id))){
            return $service->send_user_error('该用户地址不存在');
        }
        $data['def_addr']   = 1;

        $save_data = $member_addrs->set_default_addr($data,$addr_id,$member_id,$msg);
        $return['error_code'] = $save_data?0:1;
        echo $msg;
        return $return;
    }

    /**
     * 1.16	下级地区列表
     */
    public function getRegions($params,&$service){
        $mdl_regions = app::get('ectools')->model('regions');
        $region_id = $params['region_id']?$params['region_id']:1;
        $data = $mdl_regions->getAllChild($region_id);
        foreach($data as $k=>$v){
            $local_name_arr = $mdl_regions->getById($v);
            $return['region_list'][$k]['region_id'] = $v;
            $return['region_list'][$k]['region_name'] = $local_name_arr['local_name'];
        }
        return $return;
    }

    /**
     * 1.17 所有地区列表
     */
    public function getAllRegions($params,&$service){
        $mdl_regions = app::get('ectools')->model('regions');
        $data = $mdl_regions->getList('region_id,p_region_id,local_name');
        foreach($data as $k=>$v){
            $return[$k]['i'] = $v['region_id'];
            $return[$k]['p'] = $v['p_region_id'];
            $return[$k]['n'] = $v['local_name'];
        }
        return $return;
    }
}